#!/usr/bin/env python

import os
import sys

from xml.dom import minidom,Node

#=========================================================================
#
#=========================================================================
def lddinfo_generate(soft,root_path):
    soft_path = os.path.join(root_path,soft,"usr/bin/")
#    cmd = "ldd "+ root_path + soft +"/usr/bin/"+soft+" 1>/tmp/" +soft+"/usr/bin/lddinfo"
#    cmd = "ldd /tmp/"+soft+"/usr/bin/"+soft+" 1>/tmp/" +soft+"/usr/bin/lddinfo"
    cmd = "ldd "+soft_path+soft+ " 1>"+soft_path+"lddinfo"
#    print cmd
    os.system(cmd)

#=========================================================================
#
#=========================================================================

def getLddLib(soft,root_path):
    import os
    try:
        lddfile = os.path.join(root_path,soft,"usr/bin/lddinfo")
#        lddfile = "/tmp/"+soft+"/usr/bin/lddinfo"
        file = open(lddfile,"r") 
    except IOError,e:
        print >> sys.stderr, "File could not be opened",e
        sys.exit(1)
        
    lines = file.readlines()
    ldd_lib = []
    
    for line in lines:
        ldd_lib.append(line.split()[0])
    file.close()
    
#    print ldd_lib
    return ldd_lib

#=========================================================================
#
#=========================================================================

def xmlGenerate(soft):
    
    doc = minidom.Document()
    
    #create vsaas node
    vsaas = doc.createElement("vsaas")
    vsaas.setAttribute("version","1.0")
    doc.appendChild(vsaas)
    
    #create softname node
    softname = doc.createElement("softname")
    text = doc.createTextNode(soft)
    softname.appendChild(text)
    vsaas.appendChild(softname)
    
    #create description node
    description = doc.createElement("description")
    text = doc.createTextNode("just for test")
    description.appendChild(text)
    vsaas.appendChild(description)
    
    #create version node
    version = doc.createElement("version")
    text = doc.createTextNode("1.0")
    version.appendChild(text)
    vsaas.appendChild(version)
    
    #create exec_cmd node
    exec_cmd = doc.createElement("exec_cmd")
    text = doc.createTextNode("/usr/bin/" + soft)
    exec_cmd.appendChild(text)
    vsaas.appendChild(exec_cmd)
    
    
    
    
        
    #generate xml file
    return doc.toprettyxml(indent = "    ")


def xmlGenerate(soft,libfile_dict,lddlib):
    
    doc = minidom.Document()
    
    #create vsaas node
    vsaas = doc.createElement("vsaas")
    vsaas.setAttribute("version","1.0")
    doc.appendChild(vsaas)
    
    #create softname node
    softname = doc.createElement("softname")
    text = doc.createTextNode(soft)
    softname.appendChild(text)
    vsaas.appendChild(softname)
    
    #create description node
    description = doc.createElement("description")
    text = doc.createTextNode("just for test")
    description.appendChild(text)
    vsaas.appendChild(description)
    
    #create version node
    version = doc.createElement("version")
    text = doc.createTextNode("1.0")
    version.appendChild(text)
    vsaas.appendChild(version)
    
    #create exec_cmd node
    exec_cmd = doc.createElement("exec_cmd")
    text = doc.createTextNode("/usr/bin/" + soft)
    exec_cmd.appendChild(text)
    vsaas.appendChild(exec_cmd)
    
    #create library node
    library = doc.createElement("library")
    
    #create libfiles node
    for lib in libfile_dict.keys():
        
        libfile = doc.createElement("libfile")
        libfile.setAttribute("path", "/usr/lib/")
        libfile.setAttribute("name", lib)
        libfile.setAttribute("soname",libfile_dict[lib])
        if libfile_dict[lib] in lddlib:
            libfile.setAttribute("ldd", "true")
        else:
            libfile.setAttribute("ldd", "false")
        library.appendChild(libfile)
    
    vsaas.appendChild(library)
    
        
    #generate xml file
    return doc.toprettyxml(indent = "    ")

#=========================================================================
#
#=========================================================================

def getLibfiles(path):
    libfile_dict = {}
    libfiles = [f for f in os.listdir(path) if ".so" in f ]
    
    for libfile in libfiles:
        soname = libfile.split(".")
        index = soname.index("so")
        soname = ".".join(soname[:index+2])
        
        # if libfile is a symbol link,then do nothing
        if libfile == soname:
            pass
        else:
            libfile_dict[libfile] = soname
    return libfile_dict
#=========================================================================
#
#=========================================================================    

def main(libfile_dict):

#    soft = sys.argv[1]
    soft = "gpaint"
    xmldescription = xmlGenerate(soft,libfile_dict)   
#    print xmldescription
    try:
        file = open("/tmp/"+ soft + "/" + soft+".xml","w")
        print >>file,xmldescription
    except IOError,e:
         print >> sys.stderr, "File could not be opened",e
         sys.exit(1)
    file.close()

#=========================================================================
#
#=========================================================================

if __name__ == "__main__":
#    soft = sys.argv[1]
    soft = "gpaint"
    lddinfo_generate(soft,"/tmp")
    lddlib = getLddLib(soft,"/tmp")
#    print lddlib
    libfile_dict = getLibfiles("/tmp/"+soft+"/usr/lib")
#    print libfile_dict
    main(libfile_dict)
